﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Motoball.Paddles
{
    public class Racer : PaddleContent
    {
        private float boostSpeed = 0.6f;
        public Racer(Paddle paddle)
            : base(paddle)
        {
            animation = new RacerAnimation();
            paddleHeight = 178;
            paddleWidth = 530;
            maxPaddleDistance = 700;//440;
            ballHeight = 2280;
            type = "racer";
            maxInfluence = 3f;
        }

        public override void SpecialMovement(bool action)
        {
            base.SpecialMovement(action);
            if (action)
            {
                AudioManager.PlayCue("racer");
                paddle.Speed += boostSpeed;
            }
            else
                paddle.Speed = initialSpeed;
            
        }
        
        public override bool Update(GameTime gameTime)
        {
            if (!base.Update(gameTime))
            {
                if (specialMoveTimer < 0.4f || specialMoveTimer > 2.6f)
                {
                    float temp = specialMoveTimer;
                    if (specialMoveTimer > 2.6f)
                        temp = 3f - temp;
                    bloomValue = MathHelper.Lerp(1f, 30f, temp * 2.5f);
                }
                set.SetBloomValues(bloomValue);

                specialMoveTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (specialMoveTimer >= 3f)
                {
                    SpecialMovement(false);
                    specialMoveTimer = 0f;
                }
                return false;
            }
            return true;
        }

        public override void Initialize(float innerRadius)
        {
            shield = new BoneProperties[18];
            shield[0] = new BoneProperties(paddle.basicModel.model.Bones["Schild"]);
            string bla = "Chassis_Teil";
            for (int i = 1; i < 18; i++)
            {
                shield[i] = new BoneProperties(paddle.basicModel.model.Bones[bla + i.ToString()]);
            }
            base.Initialize(innerRadius);
        }
        
    }

    public class RacerAnimation : PaddleAnimation
    {
        double[] timer;
        public RacerAnimation()
            : base()
        {
            timer = new double[2];
            timer[0] = 0.01f;
            timer[1] = 0.03f;
        }
        BoneProperties exhaustLeft;
        BoneProperties exhaustRight;
        public override void Initialize(float innerRadius, Model model)
        {
            #if !XBOX
            save.fileName = "racer";
#endif
            base.Initialize(innerRadius, model);
            leftWheel = model.Bones["Rad1_Felge"];
            rightWheel = model.Bones["Rad2_Felge"];
            leftWheelTransform = leftWheel.Transform;
            rightWheelTransform = rightWheel.Transform;

            exhaustLeft = new BoneProperties(model.Bones["Auspuff1"]);
            exhaustRight = new BoneProperties(model.Bones["Auspuff2"]);

            nextPos = new Vector3[2];
            lastPos = new Vector3[2];
            posIndex = new int[2];
            posIndex[0] = 1;
            posIndex[1] = 4;
            nextPos[0] = exhaustKeyFrames[posIndex[0]] / 10f;
            nextPos[1] = exhaustKeyFrames[posIndex[1]] / 10f;
            lastPos[0] = exhaustKeyFrames[posIndex[0] - 1] / 10f;
            lastPos[1] = exhaustKeyFrames[posIndex[1] - 1] / 10f;
        }

        public override void UpdateColor(Vector3 color, BasicModel basicModel)
        {
            basicModel.diffColors["Auspuff1"] = new Vector3(0.4f);
            basicModel.diffColors["Auspuff2"] = new Vector3(0.4f);
            basicModel.diffColors["Rad1_Felge"] = Vector3.Lerp(Vector3.One, color, 0.7f);
            basicModel.diffColors["Rad2_Felge"] = Vector3.Lerp(Vector3.One, color, 0.7f);
        }
        Vector3[] nextPos;
        Vector3[] lastPos;
        int[] posIndex;
        public override void UpdateAnimation(GameTime gameTime)
        {
            for (int i = 0; i < 2; i++)
            {
                timer[i] += gameTime.ElapsedGameTime.TotalSeconds;
                //if defined time has passed - create a new random-direction exhaust is shaking towards
                if (timer[i] > 0.03f)
                {
                    timer[i] -= 0.03f;
                    posIndex[i] = (posIndex[i] + 1) % 10;
                    lastPos[i] = nextPos[i];
                    nextPos[i] = exhaustKeyFrames[posIndex[i]] / 40f;

                    //randomPos[i] = new Vector3(
                    //    (float)random.NextDouble(),
                    //    (float)random.NextDouble(),
                    //    (float)random.NextDouble()) * 3;
                }
            }
            exhaustLeft.modelBone.Transform = Matrix.CreateTranslation(
                Vector3.Lerp(lastPos[0],
                    nextPos[0],
                    (float)Math.Sin(timer[0])))
                * exhaustLeft.sourceTransform;
            exhaustRight.modelBone.Transform = Matrix.CreateTranslation(
                Vector3.Lerp(lastPos[1],
                nextPos[1],
                (float)Math.Sin(timer[1])))
                * exhaustRight.sourceTransform;
            base.UpdateAnimation(gameTime);
        }
    }
}
